<?php
class devisModel{
	private $id,$montant,$date,$client,$utilisateur;
	
	public function getId()
	{
		return $this->id;
	}
	
	public function getMontant()
	{
		return $this->montant;
	}
	
	public function getDate()
	{
		return $this->date;
	}
	
	public function getClient()
	{
		return $this->client;
	}
	
	public function getUtilisateur()
	{
		return $this->utilisateur;
	}
	
	public function setId($value)
	{
		$this->id = $value;
	}
	
	public function setMontant($value)
	{
		$this->montant = $value;
	}
	
	public function setDate($value)
	{
		$this->date = $value;
	}
	
	public function setClient($value)
	{
		$this->client = $value;
	}
	
	public function setUtilisateur($value)
	{
		$this->utilisateur = $value;
	}
	
	public function getDevis($nbrDevis)
	{
		require_once "userModel.php";
		$user = new UserModel();
		$tab_devis = array();
		$sql = "SELECT * FROM devis ORDER BY idDevis LIMIT $nbrDevis";
		$req = mysql_query($sql);
		$i =0;
		while($row = mysql_fetch_array($req))
		{
			$date_en = explode("-",$row["date"]);

			$annee = $date_en[0];
			$mois = $date_en[1];
			$jour = $date_en[2];
			
			$dateFr = $jour."/".$mois."/".$annee;
			
			$tab_devis[$i]["id"] = $row["IdDevis"];
			$tab_devis[$i]["montant"] = $row["montant"];
			$tab_devis[$i]["date"] = $dateFr;
			$tab_devis[$i]["client"] = $row["idClient"];
			$tab_devis[$i]["utilisateur"] = $user->getUser($row["idUtilisateur"]);
			$i++;
		}
		
		return $tab_devis;
	}
	
	public function deleteDevis($id_devis)
	{
		$sql = "DELETE FROM devis WHERE idDevis='".$id_devis."' ";
		$req = mysql_query($sql);
		
		if($req)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public function createDevis($devis)
	{
		$sql = "INSERT INTO devis (montant,date,idClient,idUtilisateur) VALUES ('".$devis->getMontant()."','".$devis->getDate()."','".$devis->getClient()."','".$devis->getUtilisateur()."')";
		$req = mysql_query($sql);
		
		if($req)
		{
			$devis->setId(mysql_insert_id());
			return true;
		}
		else
		{
			return false;
		}
		
	}
	
	public function getLeDevis($id_devis)
	{
		$sql = "SELECT * FROM devis WHERE idDevis='".$id_devis."'";
		$req = mysql_query($sql);
		$row = mysql_fetch_array($req);
		
		$tab_devis[$i]["id"] = $row["IdDevis"];
		$tab_devis[$i]["montant"] = $row["montant"];
		$tab_devis[$i]["date"] = $row["date"];
		$tab_devis[$i]["client"] = $row["idClient"];
		$tab_devis[$i]["utilisateur"] = $row["idUtilisateur"];
		
		return $tab_devis;
		
	}
	
}